package com.example.resubmit.domain;

import com.alibaba.fastjson.JSONObject;
import com.example.resubmit.common.Result;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Objects;


/**
 * 重复请求处理类<br>
 * <p>
 * 对加了@Resubmit的方法进行防重复处理
 */
@Component
public class ResubmitInterceptor implements MethodInterceptor {

    @Autowired
    private ResubmitLock resubmitLock;

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        boolean methodPresentAnno = Objects.requireNonNull(methodInvocation.getMethod()).isAnnotationPresent(Resubmit.class);
        if (methodPresentAnno) {
            String body = getBody();
            if (StringUtils.isNotBlank(body)) {
                final String lockKey = resubmitLock.handleKey(JSONObject.toJSONString(body));
                boolean lockRes = resubmitLock.lock(lockKey);
                if (!lockRes) {
                    Result<String> result = new Result<>();
                    result.setCode("0");
                    result.setMsg("请勿重复提交！");
                    return result;
                }
            }

        }

        return methodInvocation.proceed();
    }

    private String getBody() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        InputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }
        } catch (IOException ex) {

        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return stringBuilder.toString();
    }

}